
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="用户id:" prop="用户id">
        <el-select  v-model="formData.用户id" placeholder="请选择用户id" style="width:100%" :clearable="true" >
          <el-option v-for="(item,key) in dataSource.用户id" :key="key" :label="item.label" :value="item.value" />
        </el-select>
       </el-form-item>
        <el-form-item label="总价:" prop="总价">
          <el-input v-model.number="formData.总价" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="快递单号:" prop="快递单号">
          <el-input v-model="formData.快递单号" :clearable="true"  placeholder="请输入快递单号" />
       </el-form-item>
        <el-form-item label="状态:" prop="状态">
          <el-input v-model="formData.状态" :clearable="true"  placeholder="请输入状态" />
       </el-form-item>
        <el-form-item label="明细:" prop="明细">
          <el-input v-model="formData.明细" :clearable="true"  placeholder="请输入明细" />
       </el-form-item>
        <el-form-item label="支付订单id:" prop="支付订单id">
          <el-input v-model="formData.支付订单id" :clearable="true"  placeholder="请输入支付订单id" />
       </el-form-item>
        <el-form-item label="收件人电话:" prop="收件人电话">
          <el-input v-model="formData.收件人电话" :clearable="true"  placeholder="请输入收件人电话" />
       </el-form-item>
        <el-form-item label="收件人姓名:" prop="收件人姓名">
          <el-input v-model="formData.收件人姓名" :clearable="true"  placeholder="请输入收件人姓名" />
       </el-form-item>
        <el-form-item label="收件省份:" prop="收件省份">
          <el-input v-model="formData.收件省份" :clearable="true"  placeholder="请输入收件省份" />
       </el-form-item>
        <el-form-item label="收件城市:" prop="收件城市">
          <el-input v-model="formData.收件城市" :clearable="true"  placeholder="请输入收件城市" />
       </el-form-item>
        <el-form-item label="收件区域:" prop="收件区域">
          <el-input v-model="formData.收件区域" :clearable="true"  placeholder="请输入收件区域" />
       </el-form-item>
        <el-form-item label="收件详细地址:" prop="收件详细地址">
          <el-input v-model="formData.收件详细地址" :clearable="true"  placeholder="请输入收件详细地址" />
       </el-form-item>
        <el-form-item label="关闭时间:" prop="关闭时间">
          <el-date-picker v-model="formData.关闭时间" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
    getA订单DataSource,
  createA订单,
  updateA订单,
  findA订单
} from '@/plugin/mall/api/订单'

defineOptions({
    name: 'A订单Form'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'

const route = useRoute()
const router = useRouter()

const type = ref('')
const formData = ref({
            用户id: undefined,
            总价: undefined,
            快递单号: '',
            状态: '',
            明细: '',
            支付订单id: '',
            收件人电话: '',
            收件人姓名: '',
            收件省份: '',
            收件城市: '',
            收件区域: '',
            收件详细地址: '',
            关闭时间: new Date(),
        })
// 验证规则
const rule = reactive({
               总价 : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()
  const dataSource = ref([])
  const getDataSourceFunc = async()=>{
    const res = await getA订单DataSource()
    if (res.code === 0) {
      dataSource.value = res.data
    }
  }
  getDataSourceFunc()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findA订单({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
}

init()
// 保存按钮
const save = async() => {
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return
            let res
           switch (type.value) {
             case 'create':
               res = await createA订单(formData.value)
               break
             case 'update':
               res = await updateA订单(formData.value)
               break
             default:
               res = await createA订单(formData.value)
               break
           }
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
